<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPArray;
use PhpAmqpLib\Wire\AMQPTable;

class RunRabbitMQProducerCmd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */   
    protected $signature = 'rabitmq:producer_test {msg} {time}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'test rabbitmq producer';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(){
        $s = $this->argument('msg');
        $time = $this->argument('time')??null;
        if(empty($time)){
            $time=10;
        }
        $time=$time*1000;
$params = array(
    'mydlx',
    'mydlq',
    'mydlr',
);  
        $connection = new AMQPStreamConnection(env('AMQP_HOST','localhost'), env('AMQP_PORT',5672), env('AMQP_USER','rabbitmq'),env('AMQP_PASS','rabbitmq'));
        $channel = $connection->channel();
        $msg = new AMQPMessage($s,[
            "expiration"=>$time//这里单独设置某条消息的到期时间
        ]);
        $channel->queue_declare('activity_test',false,false,false,false,false,new AMQPTable([
            "x-dead-letter-exchange"=>$params[0],
            "dead-letter-routing-key"=>$params[2],
            //"x-message-ttl"=>10000  //如果配置这里就是整个队列里面的消息都是10秒钟
        ]));
        $channel->queue_bind('activity_test','allenapi');//这里绑定队列到交换器
        $channel->basic_publish($msg,'allenapi');
        $this->info("[x] Sent '{$s}' {$time}\n");
    }
}
